package com.dks.repository.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dks.dto.MyCourseClassDto;
import com.dks.mapper.StudentClassMapper;
import com.dks.mapper.StudentMapper;
import com.dks.models.Student;
import com.dks.repository.IStudentRepository;
import com.dks.result.Result;
import com.dks.result.ResultInfo;
import com.dks.result.ResultModel;
import com.dks.util.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class StudentRepository implements IStudentRepository {

    private StudentMapper studentMapper;

    private StudentClassMapper studentClassMapper;

    @Autowired
    public StudentRepository(final StudentMapper studentMapperIn,final StudentClassMapper studentClassMapperIn) {
        this.studentMapper = studentMapperIn;
        this.studentClassMapper = studentClassMapperIn;
    }

    @Override
    public Integer insert(Student student) {
        return studentMapper.insert(student);
    }

    @Override
    public Integer update(Student student) {
        return studentMapper.update(student);
    }

    @Override
    public Integer delete(Integer id) {
        return studentMapper.delete(id);
    }

    @Override
    public Student select(Integer id) {
        return studentMapper.select(id);
    }

    @Override
    public Integer cntByMap(Map<String, Object> map) {
        return studentMapper.cntByMap(map);
    }

    @Override
    public List<Integer> getByMap(Map<String, Object> map) {
        return studentMapper.getByMap(map);
    }

    @Override
    public List<Student> getByMapList(Map<String, Object> map) {
        return studentMapper.getByMapList(map);
    }

    @Override
    public Integer updateByCode(Student student) {
        return studentMapper.updateByCode(student);
    }

    @Override
    public Student selectByBfCode(String bfCode) {
        return studentMapper.selectByBfCode(bfCode);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取宝宝列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: parentId 家长id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getStudentList(Integer parentId, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("parentId", parentId);
        map.put("recStatus", 0);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<Student> studentList = studentMapper.getByMapList(map);
        PageInfo<Student> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), studentList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);

    }

    /**
     * @Author: Ah Nan
     * @Description: 获取宝宝详情
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: parentId 家长id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getStudentInfo(Integer id) {
        Student student = studentMapper.select(id);
        return Result.makeResult(ResultInfo.SUCCESS, student);
    }


    /**
     * @Author: Ah Nan
     * @Description: 创建宝宝
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: parentId 家长id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> insertStudentInfo(Student student) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        try {
            student.setBirthday(LocalDateTime.parse(student.getBirthdayStr()+" 00:00:00", formatter));
        }catch (Exception e) {
            e.printStackTrace();
        }
        int row = studentMapper.insert(student);
        if (row == 1) {
            return Result.makeResult(ResultInfo.SUCCESS, row);
        } else {
            return Result.makeResult(ResultInfo.ERR_DATA_STRUCTURE, row);
        }
    }

    /**
     * @Author: Ah Nan
     * @Description: 编辑宝宝
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: parentId 家长id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> updateStudentInfo(Student student) {

        if (!(student.getBirthdayStr() == null || student.getBirthdayStr().isEmpty())) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            try {
                student.setBirthday(LocalDateTime.parse(student.getBirthdayStr()+" 00:00:00", formatter));
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        int row = studentMapper.update(student);

        if (row == 1) {
            return Result.makeResult(ResultInfo.SUCCESS, row);
        } else {
            return Result.makeResult(ResultInfo.ERR_DATA_STRUCTURE, row);
        }
    }


    /**
     * @Author: Ah Nan
     * @Description: 删除宝宝
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: studentId 宝宝id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> deleteStudent(Student student) {
        student.setRecStatus(1);
        int row = studentMapper.update(student);
        if (row == 1) {
            return Result.makeResult(ResultInfo.SUCCESS, row);
        } else {
            return Result.makeResult(ResultInfo.ERR_DATA_STRUCTURE, row);
        }
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取同班同学列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: studentId 宝宝id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getclassStudentList(Integer classId, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);
        map.put("recStatus", 0);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<Student> studentList = studentMapper.getclassStudentList(map);
        PageInfo<Student> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), studentList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);

    }

    /**
     * @Author: Kenny
     * @Description: 我的课表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: studentId 家长id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getMyClassList(String beginTime, String endTime, String courseCategory, String appointmentType, String classAttendStatus, Long studentId, Long parentId, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("courseCategory", courseCategory);
        map.put("appointmentType", appointmentType);
        map.put("classAttendStatus", classAttendStatus);
        map.put("studentId", studentId);
        map.put("parentId", parentId);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<MyCourseClassDto> teacherList = studentClassMapper.myCourseList(map);
        PageInfo<MyCourseClassDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), teacherList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }

    /**
     * @Author: Kenny
     * @Description: 我的课表-详情
     * @Date: 2020-06-09
     * @Modificd By:
     * @Param: studentId 家长id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getMyClassDetail(Long studentClassId) {
        MyCourseClassDto myCourseClassDto = studentClassMapper.myCourseDetail(studentClassId);
        return Result.makeResult(ResultInfo.SUCCESS, myCourseClassDto);
    }
}
